package com.zoke.loveway.view.emoji;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.style.DynamicDrawableSpan;
import android.util.AttributeSet;
import android.widget.EditText;

import com.zoke.loveway.R;

public class EmojiconEditText extends EditText {
	private int mEmojiconSize;
	private int mEmojiconAlignment;
	private int mEmojiconTextSize;
	private boolean mUseSystemDefault = false;

	public EmojiconEditText(Context context) {
		super(context);
		mEmojiconSize = (int) getTextSize();
		mEmojiconTextSize = (int) getTextSize();
	}

	public EmojiconEditText(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(attrs);
	}

	public EmojiconEditText(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(attrs);
	}

	private void init(AttributeSet attrs) {
		TypedArray a = getContext().obtainStyledAttributes(attrs,
				R.styleable.Emojicon);
		mEmojiconSize = (int) a.getDimension(R.styleable.Emojicon_emojiconSize,
				getTextSize());
		mEmojiconAlignment = a.getInt(R.styleable.Emojicon_emojiconAlignment,
				DynamicDrawableSpan.ALIGN_BASELINE);
		mUseSystemDefault = a.getBoolean(
				R.styleable.Emojicon_emojiconUseSystemDefault, false);
		a.recycle();
		mEmojiconTextSize = (int) getTextSize();
		setText(getText());
	}

	@Override
	protected void onTextChanged(CharSequence text, int start,
			int lengthBefore, int lengthAfter) {
		updateText();
	}

	/**
	 * Set the size of emojicon in pixels.
	 */
	public void setEmojiconSize(int pixels) {
		mEmojiconSize = pixels;

		updateText();
	}

	private void updateText() {
		EmojiconHandler.addEmojis(getContext(), getText(), mEmojiconSize,
				mEmojiconAlignment, mEmojiconTextSize, mUseSystemDefault);
	}

	/**
	 * Set whether to use system default emojicon
	 */
	public void setUseSystemDefault(boolean useSystemDefault) {
		mUseSystemDefault = useSystemDefault;
	}
}
